package com.drinks.yiniang.common.recycler.decoration;

import android.graphics.Rect;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.drinks.yiniang.App;
import com.hansen.library.utils.ScreenSizeUtils;

/**
 * Created by han on 2019/6/20 0020.
 */

public class FavoriteItemDecoration extends RecyclerView.ItemDecoration {
    private int space;

    public FavoriteItemDecoration(int space) {
        this.space = space;
    }

    public static FavoriteItemDecoration getItemDPDecoration(int space) {
        space = ScreenSizeUtils.dp2px(App.getInstance().getApplicationContext(), space);

        return new FavoriteItemDecoration(space);
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {

        //通过layoutposition设置会错乱，改成GridLayoutManager.layoutParams.getSpanIndex()

        GridLayoutManager.LayoutParams params = (GridLayoutManager.LayoutParams) view.getLayoutParams();

        // Add top margin only for the first item to avoid double space between items
        if (params.getSpanIndex() == 0) {
            outRect.left = space;
            outRect.right = space / 2;
        } else {
            outRect.left = space / 2;
            outRect.right = space;
        }

        outRect.top = 0;
        outRect.bottom = space;
    }
}
